const fs = require('fs')
 let getData = (ReqData)=>{
    var get_url = 'https://mecs.fleetingpower.com/services/v3/gaiaMobile/car/query';
    var token = '', xPermId = ''
    fs.readFile('./files/token.txt','utf8',function(err, data){
        token = data
    })
    fs.readFile('./files/xPermId.txt','utf8',function(err, data){
        xPermId = data
    })
    var time = new Date().getTime()
    return new Promise((resolve,reject)=>{
    var sign = signGenerate(ReqData)
    console.log('sign',sign, 'token', token)
    superagent.post(get_url)
    // .set("Cookie", ReqData.cookie)
    .set('Content-Type','application/x-www-form-urlencoded;charset=UTF-8')
    .set('origin','https://mp-merchant.fleetingpower.com')
    .set('user-agent','Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36')
    .set('x-app-key','broker_portal')
    .set('sign',sign)
    .set('authority','mecs.fleetingpower.com')
    .set('path','/services/v3/gaiaMobile/car/query')
    .set('scheme','https')
    .set('timestamp',time)
    .set('x-perm-id',xPermId)
    .set('x-broker-token',token)
    .set('referer', 'https://mp-merchant.fleetingpower.com/')
    .send(ReqData)
    .end((err,response)=>{
      console.log('response.session', response.session)
      var data = JSON.parse(response.text)
      console.log(response.text)
      if(data.code!=200 || !data.result){
        reject(err)
        return data
      };
      var res= {}
      res = {...data.result[0], xPermId: response.headers['x-perm-id'], token: ReqData.token}
      // console.log('getData response',res)
      resolve(res);
    })
    }).then( (res) => {
        console.log('get res',  res)
        return res
    })
}

module.exports=getData